package com.jy.qnbbs.service.impl;

import com.jy.qnbbs.dao.BbsDetailDao;
import com.jy.qnbbs.entity.BbsDetail;
import com.jy.qnbbs.service.BbsDetailService;
import com.jy.qnbbs.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BbsDetailServiceImpl implements BbsDetailService {

    @Autowired
    private BbsDetailDao bbsDetailDao;

    @Override
    public Result<BbsDetail> addBbsDetail(BbsDetail bbsDetail) {
        Result<BbsDetail> result=new Result<>();

        if (bbsDetailDao.checkTitle(bbsDetail.getTitle())==0){
            int count=bbsDetailDao.saveBbsDetail(bbsDetail);
            if (count==1){
                result.setStatus(0);
                result.setMsg("发布成功！");
            }else{
                result.setStatus(1);
                result.setMsg("发布失败！");
            }
        }else {
            result.setStatus(1);
            result.setMsg("标题不能重复！");
        }

        return result;
    }

    @Override
    public Result<List<BbsDetail>> queryBbsDetailBySortId(int sortId) {
        Result<List<BbsDetail>> result=new Result<>();
        List<BbsDetail> bbsDetailList=bbsDetailDao.findBbsDetailBySortId(sortId);
        if (bbsDetailList.size()>0){
            result.setStatus(0);
            result.setData(bbsDetailList);
        }else{
           result.setStatus(1);
           result.setMsg("没有内容！");
        }
        return result;
    }

    @Override
    public Result checkTitle(String title) {
        Result result=new Result();
        int count=bbsDetailDao.checkTitle(title);
        if (count==0){
            result.setStatus(0);
        }else{
            result.setStatus(1);
            result.setMsg("标题不能重复！");
        }
        return result;
    }


}
